<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../ct-repository.html">

<link rel="import" href="../ct-commit-mock.html">

<script>
(function () {

var assert = chai.assert;

describe('ct-repository model', function() {
  it('handles response', function() {
    var commit = new CTCommitMock();
    var exampleCommitData = ')]}\n' +
      JSON.stringify({
        "log": [
          {
            "commit": "068885c2c5fda617e634bb73a107a0285af470ff",
            "tree": "750c92f0fe1294bdddbf00cc14378d0d440290cb",
            "parents": [
              "e6ba81e00ae835946e069e5bd80bd533b11d8442"
            ],
            "author": {
              "name": commit.author,
              "email": commit.author,
              "time": "Tue Jul 17 17:10:47 2012"
            },
            "committer": {
              "name": commit.author,
              "email": commit.author,
              "time": "Tue Jul 17 17:10:47 2012"
            },
            "message": commit.message,
          }
        ]
      });

    var repoUrl = 'https://mockurl/?{revision}';
    var repoName = 'blink';

    var repo = new CTRepository(repoName, '', repoUrl);
    repo._handleResponse(exampleCommitData);

    var expectedCommits = {};
    expectedCommits[commit.revision] = commit;

    assert.deepEqual(repo.commits[commit.revision], expectedCommits[commit.revision]);

    var revisions = [commit.revision];
    assert.deepEqual(repo.range(revisions), [commit]);

    // This url is different than the one above because CTCommitLog gets the url
    // from CTRepositories.
    var incompleteRepositoryUrl = repo.repositoryUrl;

    var nextRev = String(commit.revision + 1);
    var incompleteCommitAfter =
        CTCommit.createIncomplete(incompleteRepositoryUrl, nextRev, repoName);
    revisions.push(nextRev);
    // Expect an incomplete commit for revision + 1.
    assert.deepEqual(repo.range(revisions), [commit, incompleteCommitAfter]);

    // Expect two incomplete commits: one before and one after.
    var prevRev = String(commit.revision - 1);
    var incompleteCommitBefore =
        CTCommit.createIncomplete(incompleteRepositoryUrl, prevRev, repoName);
    // Inserting at the beginning of the list.
    revisions.splice(0, 0, prevRev);
    assert.deepEqual(repo.range(revisions),
                     [incompleteCommitBefore, commit, incompleteCommitAfter]);
  });
});

})();
</script>
